package cn.sykj.car.controller;


import cn.sykj.car.entity.Operation;
import cn.sykj.car.entity.Roles;
import cn.sykj.car.entity.Users;
import cn.sykj.car.service.IMenuService;
import cn.sykj.car.utils.Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author Yixu
 * @since 2018-11-29
 */
@Api(hidden = false, tags = {"角色 前端控制器"})
@RestController
@RequestMapping("/roles")
public class RolesController extends BaseController{
    // 日志
    private static Logger logger = LoggerFactory.getLogger(RolesController.class);

    @Resource
    private cn.sykj.car.service.IRolesService iRolesService;

    @Resource
    private cn.sykj.car.service.IUsersService iUsersService;
    @Resource
    private IMenuService iMenuService;
    @Resource
    private cn.sykj.car.service.IOperationService iOperationService;


    /**
     * 查询角色：登陆后才能操作
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "查询角色", notes = "登陆才能操作")
    @RequiresAuthentication
    @RequiresPermissions("roles:view")
    @RequestMapping(value = "/selectRoles", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean selectRoles(@RequestBody Roles rolesParam) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);

        if (Utils.notEmpty(rolesParam.getId())) {

            // 查询单条角色
            Roles roles = iRolesService.getById(rolesParam.getId());
            return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK, roles);
        } else {

            cn.sykj.car.vo.RolesVO searchRoles = new cn.sykj.car.vo.RolesVO();
            searchRoles.setId(rolesParam.getId());
            if(Utils.notEmpty(rolesParam.getRoleName())){
                searchRoles.setRoleName(rolesParam.getRoleName());
            }

            cn.sykj.car.vo.RolesVO rolesVO = iRolesService.selectAllRoles(searchRoles);
            return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK, rolesVO);
        }

    }

    /**
     * 查询角色：登陆后才能操作
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "根据用户查询角色", notes = "登陆才能操作")
    @RequiresAuthentication
    @RequiresPermissions("roles:view")
    @RequestMapping(value = "/selectRolesByUser", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean selectRolesByUser(
    ) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);
        Users users = iUsersService.getUserByName(obj.toString());
        // 按所属公司查询角色
        QueryWrapper rolesCondition = new QueryWrapper();
        rolesCondition.eq(Roles.ID,users.getId());
        List<Roles> rolesList = iRolesService.list(rolesCondition);
        return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK, rolesList);
    }

    /**
     * 根据角色id查询用户
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "根据角色id查询用户", notes = "登陆才能操作")
    @RequiresAuthentication
    @RequiresPermissions("roles:view")
    @RequestMapping(value = "/selectUsersByRoleId", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean selectUsersByRoleId(@RequestBody Roles rolesParam) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);

        List<Users> usersList = iUsersService.selectUsersByRoleId(rolesParam.getId());
        return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK, usersList);
    }

    /**
     * 根据角色id查询菜单
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "根据角色id查询菜单", notes = "登陆才能操作")
    @RequiresAuthentication
    @RequiresPermissions("roles:view")
    @RequestMapping(value = "/selectMenusByRoleId", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean selectMenusByRoleId(
            @RequestBody cn.sykj.car.vo.RolesVO rolesParam
    ) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);
        List<cn.sykj.car.vo.SysMenuVO> list = iMenuService.selectMenusByRoleId(rolesParam);
        return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK, list);
    }

    /**
     * 根据角色id查询操作权限
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "根据角色id查询操作权限", notes = "登陆才能操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "Integer", paramType = "query",required = true),
    })
    @RequiresAuthentication
    @RequiresPermissions("roles:view")
    @RequestMapping(value = "/selectOperationsByRoleId", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean selectOperationsByRoleId(
            @RequestBody Roles rolesParam
    ) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);
        List<Operation> list = iOperationService.selectOperationsByRoleId(rolesParam.getId());
        return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK,list);
    }


    /**
     * 添加角色，登陆后才能操作
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "添加角色", notes = "登陆才能操作")
    @RequiresAuthentication
    @RequiresPermissions("roles:create")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean insertRoles(
            @RequestBody cn.sykj.car.vo.RolesVO rolesParam, HttpServletRequest request
    ) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);

        // 插入角色
        cn.sykj.car.vo.RolesVO roles = new cn.sykj.car.vo.RolesVO();

        if(Utils.notEmpty(rolesParam.getRoleName())){
            roles.setRoleName(rolesParam.getRoleName());
        }
        if (Utils.notEmpty(rolesParam.getUserIdList())) {
            roles.setUserIdList(rolesParam.getUserIdList());
        }
        if (Utils.notEmpty(rolesParam.getMenuIdList())) {
            roles.setMenuIdList(rolesParam.getMenuIdList());
        }
        if (Utils.notEmpty(rolesParam.getOperationIdList())) {
            roles.setOperationIdList(rolesParam.getOperationIdList());
        }

        if(Utils.notEmpty(rolesParam.getPid())){
            roles.setPid(rolesParam.getPid());
        }else{
            roles.setPid(0);
        }

        this.writeLog("添加角色");

        iRolesService.insertRoles(roles);
        return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK,null);
    }


    /**
     * 修改角色，登陆后才能操作
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "修改角色", notes = "登陆才能操作")
    @RequiresAuthentication
    @RequiresPermissions("roles:edit")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean updateRoles(
            @RequestBody cn.sykj.car.vo.RolesVO rolesParam, HttpServletRequest request
    ) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);

        // 修改角色
        cn.sykj.car.vo.RolesVO roles = new cn.sykj.car.vo.RolesVO();

        if(Utils.notEmpty(rolesParam.getRoleName())){
            roles.setRoleName(rolesParam.getRoleName());
        }

        if(Utils.notEmpty(rolesParam.getId())){
            roles.setId(rolesParam.getId());
        }

        if(Utils.notEmpty(rolesParam.getPid())){
            roles.setPid(rolesParam.getPid());
        }

        if (Utils.notEmpty(rolesParam.getUserIdList())) {
            roles.setUserIdList(rolesParam.getUserIdList());
        }
        if (Utils.notEmpty(rolesParam.getMenuIdList())) {
            roles.setMenuIdList(rolesParam.getMenuIdList());
        }
        if (Utils.notEmpty(rolesParam.getOperationIdList())) {
            roles.setOperationIdList(rolesParam.getOperationIdList());
        }

        this.writeLog("修改角色");

        iRolesService.updateRoles(roles);
        return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK,null);
    }

    /**
     * 删除角色，登陆后才能操作
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "删除角色", notes = "登陆才能操作")
    @RequiresAuthentication
    @RequiresPermissions("roles:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean deleteRoles(
            @PathVariable Integer id,HttpServletRequest request
    ) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);

        // 插入角色
        cn.sykj.car.vo.RolesVO roles = new cn.sykj.car.vo.RolesVO();
        roles.setId(id);

        this.writeLog("删除角色");
        iRolesService.deleteRoles(roles);
        return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK,null);
    }

}
